package com.tduck.cloud.security.service;

import com.tduck.cloud.common.entity.security.SysUserVO;
import com.tduck.cloud.common.util.SecurityUtils;
import com.tduck.cloud.system.service.SysMenuService;
import com.tduck.cloud.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户权限处理
 *
 * @author smalljop
 */
@Component
@RequiredArgsConstructor
public class SysPermissionService {
    private final SysRoleService roleService;

    private final SysMenuService menuService;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUserVO user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin(user.getId())) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.getRolePermissionByUserId(user.getId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUserVO user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin(user.getId())) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.getMenuPermsByUserId(user.getId()));
        }
        return perms;
    }
}
